package com.radiant.data.entity;

import java.util.List;

import org.apache.commons.beanutils.ConvertUtilsBean;

import com.google.inject.Inject;
import com.radiant.data.entity.converter.SimpleDBEntityConverter;

public class PostDAOImpl extends SimpleDBDAO<Post> implements PostDAO {

	@Inject
	private CommentDAO commentDAO;

	public PostDAOImpl() {
		super(Post.class);
	}

	@Override
	protected void registerConverters(ConvertUtilsBean convertUtilsBean) {
		convertUtilsBean.register(new Like.LikeConverter(), PostLike.class);
		convertUtilsBean.register(new SimpleDBEntityConverter<CommentDAO>(preProcessor, commentDAO), Comment.class);		
	}

	public List<Post> getTopNPosts(String userName, int count) {
		StringBuffer whereClause = new StringBuffer("userName = '");
		whereClause.append(userName).append("'");
		whereClause.append(" AND timeUpdated is not null");
		return query(whereClause.toString(), "timeUpdated", count, false);
	}

	public List<Post> getPostsInTimeInterval(String userName,
			long startTimeInMillis, long endTimeInMillis) {
		StringBuffer whereClause = new StringBuffer("userName = '").append(userName).append("'");
		whereClause.append(" AND ");
		whereClause.append("timeUpdated >= ").append(startTimeInMillis);
		whereClause.append(" AND ");
		whereClause.append("timeUpdated < ").append(endTimeInMillis);
		return query(whereClause.toString(), "timeUpdated", 0, false);
	}

	public List<Post> getRecentPosts(String userName, long timeInMillis) {
		StringBuffer whereClause = new StringBuffer("userName = '").append(userName).append("'");
		whereClause.append(" AND ");
		long queryTime = System.currentTimeMillis() - timeInMillis;
		whereClause.append("timeUpdated >= ").append(queryTime);
		return query(whereClause.toString(), "timeUpdated", 0, false);
	}

}
